<?php
class M_daftaranggaran extends CI_Model {
	
	function getlist($filter,$start,$end){
		$this->db->offset($start);
		$this->db->limit($end);
		$sql ="SELECT*,
			if((pengajuan_status)='P','Pengajuan',
			if((pengajuan_status)='AKP','Disetujui Kepala Pusat',
			if((pengajuan_status)='AKB','Disetujui Kepala Badan',''))) as p_status 
				FROM t_pengajuan
				LEFT JOIN
				(
				SELECT t_pengajuan_id, sum(t_pengajuandet.pengajuandet_value) AS t FROM `t_pengajuandet`
				group by t_pengajuan_id
				) t on t_pengajuan.pengajuan_id = t_pengajuan_id
				LEFT JOIN 
				m_anggaran on t_pengajuan.m_anggaran_id = m_anggaran.anggaran_id
				";
		if(!empty($filter)){
			$sql .= "WHERE lower(`anggaran_kegiatan`)  like '%".addslashes(strtolower($filter))."%'";
		}
		$query = $this->db->query($sql);
		$total = $query->num_rows();
		$arr = array();
		foreach($query->result() as $q){
			$arr[] = $q; 
		}
		$final = array(
						'total'   => $total,
						'results' => $arr
					  );
		return json_encode($final);
	}
	
	function delete($pengajuan_id){
		$this->db->where('pengajuan_id',$pengajuan_id);
		$this->db->delete('t_pengajuan');
		echo 1;
	}
}